package poi_export;

import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author yujiafan
 * @version 1.0.0
 * @create 2022-12-30
 */
public class WordHWPF {

    @Test
    public void contextLoads() throws IOException {
        // 创建一个.docx文档
        XWPFDocument document = new XWPFDocument();
        // 创建段落-文本
        // 第一段
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);      // 居中
        XWPFRun paragraphRun = paragraph.createRun();
        paragraphRun.setBold(true);     // 加粗
        paragraphRun.setFontSize(30);   // 字体大小
        paragraphRun.setFontFamily("黑体");   // 字体
        paragraphRun.setText("**公司分析报告");   // 文本内容

        XWPFParagraph paragraph1 = document.createParagraph();
        paragraph1.setIndentationFirstLine(400);    //首行缩进2字符（大概400）
        XWPFRun paragraphRun1 = paragraph1.createRun();
        paragraphRun1.setFontSize(25);
        paragraphRun1.setFontFamily("黑体");
        paragraphRun1.setText("一、基本情况");

        XWPFParagraph paragraph2 = document.createParagraph();
        paragraph2.setIndentationFirstLine(400);
        XWPFRun paragraphRun2 = paragraph2.createRun();
        paragraphRun2.setFontFamily("宋体");
        paragraphRun2.setText("**公司（纳税人识别号：**）成立于**年**月**日，注册类型为**，主要投资方是**,公司所属行业为**，纳税信用等级为**级，注册地在**。");

        XWPFParagraph paragraph3 = document.createParagraph();
        paragraph3.setIndentationFirstLine(400);
        XWPFRun paragraphRun3 = paragraph3.createRun();
        paragraphRun3.setFontFamily("宋体");
        paragraphRun3.setText("（一）经营情况和税收贡献情况");

        XWPFParagraph paragraph4 = document.createParagraph();
        paragraph4.setAlignment(ParagraphAlignment.CENTER);      // 居中
        XWPFRun paragraphRun4 = paragraph4.createRun();
        paragraphRun4.setFontFamily("宋体");   // 字体
        paragraphRun4.setText("近*年营收情况");   // 文本内容
        // -----------------------------------------------------------------
        XWPFParagraph paragraph5 = document.createParagraph();
        paragraph5.setAlignment(ParagraphAlignment.RIGHT);      // 居中
        XWPFRun paragraphRun5 = paragraph5.createRun();
        paragraphRun5.setFontFamily("宋体");   // 字体
        paragraphRun5.setText("单位：万元");   // 文本内容
        // 创建表格
        XWPFTable table = document.createTable();
        table.setWidth("100%");
        XWPFTableRow row0 = table.getRow(0);
        XWPFTableCell cell0 = row0.getCell(0);
        cell0.setText("年度");
        row0.addNewTableCell().setText("2019年");  // 添加列
        row0.addNewTableCell().setText("2020年");  // 添加列
        row0.addNewTableCell().setText("2021年");  // 添加列
        String[] strings = {"营业收入","营业成本","营业利润"};
        for (int i = 0; i < strings.length; i++) {
            XWPFTableRow row0_0 = table.createRow();  // 添加行
            row0_0.getCell(0).setText(strings[i]);
            row0_0.getCell(1).setText("");
            row0_0.getCell(2).setText("");
            row0_0.getCell(3).setText("");
        }

        XWPFParagraph paragraph6 = document.createParagraph();
        paragraph6.setAlignment(ParagraphAlignment.CENTER);      // 居中
        XWPFRun paragraphRun6 = paragraph6.createRun();
        paragraphRun6.setFontFamily("宋体");   // 字体
        paragraphRun6.setText("近**年主要税种税收净入库情况");   // 文本内容
        // -----------------------------------------------------------------
        XWPFParagraph paragraph7 = document.createParagraph();
        paragraph7.setAlignment(ParagraphAlignment.RIGHT);      // 居中
        XWPFRun paragraphRun7 = paragraph7.createRun();
        paragraphRun7.setFontFamily("宋体");   // 字体
        paragraphRun7.setText("单位：万元");   // 文本内容
        // 创建表格
        XWPFTable table1 = document.createTable();
        table1.setWidth("100%");
        XWPFTableRow row1 = table1.getRow(0);
        XWPFTableCell cell1 = row1.getCell(0);
        cell1.setText("税种");
        row1.addNewTableCell().setText("2019年");  // 添加列
        row1.addNewTableCell().setText("2020年");  // 添加列
        row1.addNewTableCell().setText("2021年");  // 添加列
        row1.addNewTableCell().setText("2022年");  // 添加列
        String[] strings1 = {"合计","增值税","其中：代扣缴增值税","企业所得税","其中：代扣缴企业所得税","房产税","个人所得税"};
        for (int i = 0; i < strings1.length; i++) {
            XWPFTableRow row1_1 = table1.createRow();  // 添加行
            row1_1.getCell(0).setText(strings1[i]);
            row1_1.getCell(1).setText("");
            row1_1.getCell(2).setText("");
            row1_1.getCell(3).setText("");
            row1_1.getCell(4).setText("");
        }

        XWPFParagraph paragraph8 = document.createParagraph();
        paragraph8.setIndentationFirstLine(400);    //首行缩进2字符（大概400）
        XWPFRun paragraphRun8 = paragraph1.createRun();
        paragraphRun8.setFontSize(25);
        paragraphRun8.setFontFamily("黑体");
        paragraphRun1.setText("（二）增值税留抵税额变动情况");

        // 折线图(未实现)


        File file = new File("D:\\test.docx");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        document.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("文件创建成功");
    }
}
